package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Options;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.StadiumResult;
import com.iot.ic.model.TeamInfo;
import com.iot.ic.service.StadiumResultService;
import com.iot.ic.service.TeamService;
import com.iot.ic.tcp.model.MsgResponse;
import com.iot.ic.tcp.netty.CustomHandler;

import io.netty.channel.ChannelHandlerContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api(description = "车队信息相关接口")
@RestController
public class TeamController extends BaseController {

    @Autowired
    private TeamService teamService;
    
    @Autowired
    private StadiumResultService stadiumResultService;
    
    


    /**
     * 获得车队信息列表
     *
     * @param userId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获得车队信息列表", notes = "")
    @ResponseBody
    @RequestMapping(value = "/users/{userId}/teams", method = RequestMethod.GET)
    public Response listRefereeInfos(@PathVariable("userId") int userId,
                                     @RequestParam(value = "currentPage") int currentPage,
                                     @RequestParam(value = "pageSize") int pageSize) {
        Response response = new Response();
        TeamInfo teamInfo = new TeamInfo();
        teamInfo.setCurrentPage(currentPage);
        teamInfo.setLength(pageSize);
        List<TeamInfo> teamList = teamService.listTeamInfos(teamInfo);
        response.setResult(teamList);
        
//        MsgResponse sresponse= new MsgResponse();
//        ChannelHandlerContext ctx = CustomHandler.channelMap.get("test");
//        ctx.writeAndFlush(sresponse);
        
        return response;
    }


    @ApiOperation(value = "增加参赛队伍信息", notes = "")
    @PostMapping(value = "/users/{userId}/teams")
    public Response addTeam(@PathVariable(value = "userId") Integer userId, @RequestBody @Valid TeamInfo teamInfo) {
        Response response=new Response();
        teamInfo.setCreateTime(new Date());
        teamInfo.setDelFlag(Options.NotDel.getCode());
        int result=teamService.addTeam(teamInfo);
        if (result==1)
            response.setInfos(ReturnCode.No_AddTeamOK);
        else
            response.setInfos(ReturnCode.Err_AddTeamFailor);
        return  response;
    }

    @ApiOperation(value = "编辑参赛队伍信息", notes = "")
    @PutMapping(value = "/users/{userId}/teams")
    public Response modTeam(@PathVariable(value = "userId") Integer userId, @RequestBody @Valid TeamInfo teamInfo){
        Response response=new Response();
        int result=teamService.updateTeam(teamInfo);
        if (result==1)
            response.setInfos(ReturnCode.No_UpdateTeamOK);
        else
            response.setInfos(ReturnCode.Err_UpdateTeamFailor);
        return  response;
    }

    @ApiOperation(value = "删除参赛队伍信息", notes = "")
    @DeleteMapping(value = "/users/{userId}/teams/{teamId}")
    public Response delTeam(@PathVariable(value = "userId")Integer userId,@PathVariable(value = "teamId")Integer teamId){
        Response response=new Response();
        int result=teamService.deleteTeam(teamId);
        if (result==1)
            response.setInfos(ReturnCode.No_DeleteTeamOK);
        else
            response.setInfos(ReturnCode.Err_DeleteTeamFailor);
        return  response;
    }
    
    @ApiOperation(value = "根据Id获得参赛队伍详细信息", notes = "")
    @RequestMapping(value = "/users/{userId}/teams/{teamId}", method = RequestMethod.GET)
    public Response getTeam(@PathVariable(value = "userId")Integer userId,@PathVariable(value = "teamId")Integer teamId){
        Response response = new Response();
        TeamInfo result = teamService.getTeam(teamId);
        response.setResult(result);
        return  response;
    }
    
    
	@ApiOperation(value="获得车队信息包括裁判", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/teamInfoWithReferee/{teamId}", method=RequestMethod.GET)
	public Response getTeamInfoWithReferee(@PathVariable("userId")int userId,
			@PathVariable("teamId")int teamId){
		Response response = new Response();
		TeamInfo teamInfo = teamService.getTeam(teamId);
		if (teamInfo != null) {
			StadiumResult stadiumResult = stadiumResultService.getStadiumResultLeastByTeamId(teamId);
			if (stadiumResult != null) {
				teamInfo.setRefereeName(stadiumResult.getRefereeName());
			} else {
				teamInfo.setRefereeName("");
			}
		}
		response.setResult(teamInfo);
		return response;
	}
}
